package com.qire.manhua.model.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

/**
 * 商品市场实体对象，市场包含可购买的商品信息。
 */
public class MarketInfo {

    public final boolean isFirstPay;
    public final ArrayList<WaresEntity> waresEntityList = new ArrayList<>();

    private WaresEntity newcomerWares;

    private int failureCountdown = 0;

    public MarketInfo(boolean isFirstPay){
        this.isFirstPay = isFirstPay;

    }

    public void setNewcomerWares(WaresEntity newcomerWares){
        this.newcomerWares = newcomerWares;
    }

    public WaresEntity getNewcomerWares(){
        return newcomerWares;
    }

    public void setFailureCountdown(int failureCountdown) {
        this.failureCountdown = failureCountdown;
    }

    public boolean countdownIsOver() {
        return failureCountdown <= 0;
    }

    public String calculateCountdown() {
        failureCountdown--;
        if (failureCountdown <= 0)
            return "已失效";
        return String.format("%02d分%02d秒后失效", failureCountdown / 60, failureCountdown % 60);
    }

    public class WaresEntity{
        public final int fee;         //金币费用：该商品购买所需费用
        public final int goldCoin;    //金币数量：该商品实际对待金币
        public final int rewardFee;   //金豆价值：该商品赠送奖励价值
        public final int rewardBean;  //金豆数量：该商品赠送奖励金豆

        public int productId;

        public WaresEntity(int fee,int goldCoin,int rewardFee,int rewardBean){
            this.fee         = fee;
            this.goldCoin    = goldCoin;
            this.rewardFee   = rewardFee;
            this.rewardBean  = rewardBean;
        }

        public String getDescribe(){
            String describe = goldCoin + "金币";
            describe += rewardBean > 0 ? "+" + rewardBean + "金豆" : "";
            return describe;
        }

        public String getRewardRate(){
            if(rewardFee<=0)
                return "0";
            if(fee<=0)
                return "0";
            return new BigDecimal(rewardFee).divide(new BigDecimal(fee),2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(0).toString();
        }

        public WaresEntity setProductId(int productId) {
            this.productId = productId;
            return this;
        }

        public OrderEntity buildOrderEntity(int bookId) {
            return new OrderEntity(String.valueOf(fee), productId, bookId);
        }

    }

}